package cn.zjj.product.controller;

import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.util.JsonResult;
import cn.zjj.basic.util.PageList;
import cn.zjj.product.domain.Product;
import cn.zjj.product.query.ProductQuery;
import cn.zjj.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Api(value = "服务的API",description="服务相关的CRUD功能")
public class ProductController {

    @Autowired
    private IProductService productService;


    @PostMapping("/onsale")
    @ApiOperation(value = "上架服务" )
//    @PreAuthorize(name = "上架服务",value = "product:onsale")
    public JsonResult onsale(@RequestBody List<Long> ids){
        try {
            productService.onsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }
    @PostMapping("/offsale")
    @ApiOperation(value = "下架服务" )
//    @PreAuthorize(name = "下架服务",value = "product:offsale")
    public JsonResult offnsale(@RequestBody List<Long> ids){
        try {
            productService.offsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }







    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
//    @PreAuthorize(name = "服务列表",value = "product:list")
    public List<Product> selectAll(){

        return productService.selectAll();
    }

    //查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据" )
//    @PreAuthorize(name = "查询服务",value = "product:get")
    public Product selectById(@PathVariable("id") Long id){
        return productService.selectById(id);
    }

    //删除单个
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个数据" )
//    @PreAuthorize(name = "删除服务",value = "product:delete")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //添加或修改
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
//    @PreAuthorize(name = "服务保存",value = "product:save")
    public JsonResult updateOrAdd(@RequestBody Product product){
        try {
            if(product.getId()==null){
                productService.insert(product);
            }else {
                productService.update(product);
            }
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new JsonResult().runtimeException(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询或高级查询" )
    public PageList<Product> queryPage(@RequestBody ProductQuery query){
        return productService.selectQuery(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
//    @PreAuthorize(name = "服务批量删除",value = "product:patchDel")
    public JsonResult delete(@RequestBody List<Long> ids){
        try {
            productService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }
}
